from pathlib import Path
import os

BASE_DIR = Path(__file__).resolve().parent.parent
SECRET_KEY = 'django-insecure-n@zlt+=56_8k$l*e1)46y7x7*ei-!s5u202kh-k81pls%-=7#^'

DEBUG = True
ALLOWED_HOSTS = ['*']

INSTALLED_APPS = [
    'simpleui',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django_celery_beat',  # celery定时任务
    'django_celery_results',  # celery任务结果
    'corsheaders',  # 设置跨域
    'rest_framework',  # drf
    'tasks',
    'bookstore',
    'login',
    'ancientBooks',
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    # 'django.middleware.csrf.CsrfViewMiddleware', # CSRF验证
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'corsheaders.middleware.CorsMiddleware',
    # 请求拦截中间件
    'login.middleware.MyMiddle',
]
ROOT_URLCONF = 'App.urls'

# 允许跨域源
CORS_ORIGIN_ALLOW_ALL = True
# 跨域增加忽略
CORS_ALLOW_CREDENTIALS = True
CORS_ORIGIN_WHITELIST = (
    ['http://127.0.0.1:*']
)

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [BASE_DIR / 'templates']
        ,
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'App.wsgi.application'

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3',
    }
}

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
        'OPTIONS': {
            'min_length': 8,
        }
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]

REST_FRAMEWORK = {
    'DEFAULT_PAGINATION_CLASS':
        'App.pagination.DefaultNumberPagination'
    ,  # drf分页配置
    'DEFAULT_FILTER_BACKENDS': [
        'django_filters.rest_framework.DjangoFilterBackend',
    ],  # drf过滤配置
}

LANGUAGE_CODE = 'zh-hans'
USE_TZ = False  # 这里务必调整为False，否则时区设置无效
# LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

STATIC_URL = '/static/'
# 定义静态资源位置
STATICFILES_DIRS = [os.path.join(BASE_DIR, "static"), ]
# STATIC_ROOT = os.path.join(BASE_DIR, 'static')
# MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

# 隐藏右侧SimpleUI广告链接和使用分析
SIMPLEUI_HOME_INFO = False
SIMPLEUI_ANALYSIS = False

# SIMPLEUI_CONFIG = {
#     'system_keep': False, # 关闭系统菜单
#     'menu_display': ['任务管理', '测试', '权限认证'],
#     'dynamic': True,    # 设置是否开启动态菜单, 默认为False. 如果开启, 则会在每次用户登陆时动态展示菜单内容
#     'menus': [{
#         'app': 'myapp',
#         'name': '任务管理',
#         'icon': 'fas fa-user-shield',
#         'models': [{
#             'name': '任务管理1',
#             'icon': 'fa fa-user',
#             'url': 'tasks/'
#         }]
#     },{
#         'app': 'auth',
#         'name': '权限认证',
#         'icon': 'fas fa-user-shield',
#         'models': [{
#             'name': '用户',
#             'icon': 'fa fa-user',
#             'url': 'auth/user/'
#         }]
#     }, {
#         'name': '测试',
#         'icon': 'fa fa-file',
#         'models': [{
#             'name': 'Baidu',
#             'url': 'http://baidu.com',
#             'icon': 'far fa-surprise'
#         }, {
#             'name': '内网穿透',
#             'url': 'https://www.wezoz.com',
#             'icon': 'fab fa-github'
#         }]
#     }]
# }

# 对应的发送的请求的跨域
CORS_ALLOW_METHODS = (
    'DELETE',
    'GET',
    'OPTIONS',
    'POST',
    'PUT',
)

# 允许跨域携带的请求头部信息
CORS_ALLOW_HEADERS = (
    'accept',
    'accept-encoding',
    'authorization',
    'content-type',
    'origin',
    'user-agent',
    'x-csrftoken',
    'x-requested-with',
    'Token',
)

# ----------------------------------celery配置相关---------------------------------------
# 最重要的配置，设置消息broker,格式为：db://user:password@host:port/dbname
# 如果redis安装在本机，使用localhost
# 如果docker部署的redis，使用redis://redis:6379
CELERY_BROKER_URL = "redis://127.0.0.1:6379/0"

# 设置celery时区
CELERY_TIMEZONE = TIME_ZONE

# 将任务调度器设为DatabaseScheduler
CELERY_BEAT_SCHEDULER = 'django_celery_beat.schedulers:DatabaseScheduler'

# 避免启动定时任务的时候报错
CELERY_ENABLE_UTC = False
DJANGO_CELERY_BEAT_TZ_AWARE = False

# 支持数据库django-db和缓存django-cache存储任务状态及结果
CELERY_RESULT_BACKEND = "django-db"

# celery内容等消息的格式设置，默认json
CELERY_ACCEPT_CONTENT = ['application/json', ]
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'
